import React from 'react';
import classNames from 'classnames';
import { formatMessage } from 'umi';
import { Button, message, Popconfirm, Tooltip } from 'antd';

import BizIcon from '@/components/BizIcon';
import { isSuccess } from '@/utils/request';
import If from '@/components/If';
import authHelper from '@/utils/auth/helper';

import { commonColumn, amountSettingColumn } from '@/pages/SalaryManage/utils/TableUtil';
import ExcelListBaseModel from '@/pages/SalaryManage/SalaryCalculate/ImportExcel/ExcelListBaseModel';
import { goToSpecialTaxLog, goToSpecialTaxEdit } from '@/pages/SalaryManage/utils/pageNavigateUtil';
import {
    downloadSpecialTax,
    deleteSpecialTax,
    querySpecialTaxList,
    querySpecialTaxYearMonth
} from '@/services/salaryManage/salaryCalculate';
import AutoDivider from '@/pages/SalaryManage/components/AutoDivider';
import EmptyPlaceHolder from '@/pages/SalaryManage/components/EmptyPlaceHolder';
import salaryStyle from '@/pages/SalaryManage/salaryManage.less';

const salarySpecialDeductModel = ExcelListBaseModel({
    namespace: 'salarySpecialDeductModel',
    queryService: querySpecialTaxList,
    initService: querySpecialTaxYearMonth
});

salarySpecialDeductModel.state.goToLog = goToSpecialTaxLog;
salarySpecialDeductModel.state.goToEdit = goToSpecialTaxEdit;
salarySpecialDeductModel.state.columns = (pagination, { onEdit, onDelete }) => [
    {
        // 序号
        title: formatMessage({ id: 'salaryBaseKpi.table.sn' }),
        dataIndex: 'sn',
        render(text, record, index) {
            return `${(pagination.current - 1) * pagination.pageSize + index + 1}`;
        },
        align: 'center',
        width: 80
    },
    commonColumn({
        // 工号
        title: formatMessage({ id: 'salarySpecialDeduct.table.employeeNo' }),
        dataIndex: 'employeeNo',
        align: 'left',
        width: 120
    }),
    commonColumn({
        // 姓名
        title: formatMessage({ id: 'salarySpecialDeduct.table.name' }),
        dataIndex: 'name',
        align: 'left',
        width: 132
    }),
    commonColumn({
        // 部门
        title: formatMessage({ id: 'salarySpecialDeduct.table.department' }),
        dataIndex: 'departmentName',
        width: 180
    }),
    {
        // 计薪月份
        title: formatMessage({ id: 'salarySpecialDeduct.table.period' }),
        dataIndex: 'yearMonth',
        render(text) {
            return <EmptyPlaceHolder value={text}/>
        },
        width: 100
    },
    commonColumn({
        // 截止累计数
        title: formatMessage({ id: 'salarySpecialDeduct.table.cutoffCount' }),
        dataIndex: 'kpiNum',
        align: 'center',
        children: [
            // 子女教育
            amountSettingColumn({
                title: formatMessage({ id: 'salarySpecialDeduct.table.childEducationAmt' }),
                dataIndex: 'childEducationAmt',
                width: 150
            }),
            // 继续教育
            amountSettingColumn({
                title: formatMessage({ id: 'salarySpecialDeduct.table.continueEducationAmt' }),
                dataIndex: 'continueEducationAmt',
                width: 150
            }),
            // 住房贷款利息
            amountSettingColumn({
                title: formatMessage({ id: 'salarySpecialDeduct.table.houseLoanInterestAmt' }),
                dataIndex: 'houseLoanInterestAmt',
                width: 150
            }),
            // 住房租金
            amountSettingColumn({
                title: formatMessage({ id: 'salarySpecialDeduct.table.houseLeaseAmt' }),
                dataIndex: 'houseLeaseAmt',
                width: 150
            }),
            // 赡养老人
            amountSettingColumn({
                title: formatMessage({ id: 'salarySpecialDeduct.table.raiseOldAmt' }),
                dataIndex: 'raiseOldAmt',
                width: 150
            }),
            // 合计
            amountSettingColumn({
                title: formatMessage({ id: 'salarySpecialDeduct.table.totalSpecialAmt' }),
                dataIndex: 'totalSpecialAmt',
                width: 150
            })
        ]
    }),
    {
        title: '操作',
        dataIndex: 'v_operate',
        fixed: 'right',
        width: 100,
        render(text, record) {
            return (
                <AutoDivider>
                    <If condition={authHelper.checkCode('finance:salary:computation:import:taxspecial:edit')}>
                        <Tooltip title="编辑">
                            <Button
                                className={classNames(salaryStyle.operateIcon)}
                                onClick={() => onEdit(record)}
                                disabled={!!record?.payrollIfProcessed}
                            >
                                <BizIcon type="listfunction_edit_icon_default" />
                            </Button>
                        </Tooltip>
                    </If>

                    <If condition={authHelper.checkCode('finance:salary:computation:import:taxspecial:del')}>
                        <Popconfirm
                            title={`是否确定删除【${record.name}】的专项扣款信息`}
                            placement="bottomRight"
                            onConfirm={() => onDelete(record)}
                            disabled={!!record?.payrollIfProcessed}
                        >
                            <Tooltip title="删除" placement="top">
                                <Button
                                    className={classNames(salaryStyle.operateIcon)}
                                    disabled={!!record?.payrollIfProcessed}
                                >
                                    <BizIcon type="DeleteIcon" />
                                </Button>
                            </Tooltip>
                        </Popconfirm>
                    </If>
                </AutoDivider>
            );
        }
    }
];
salarySpecialDeductModel.effects.downloadTemplate = function* downloadExcel({ payload }, { call }) {
    return yield call(downloadSpecialTax, payload);
};

salarySpecialDeductModel.effects.deleteSpecialTax = function* deleteBaseKpi(
    { payload, callback, currentRecord },
    { call, put }
) {
    yield put({
        type: 'setMultiStore',
        payload: { pageLoading: true }
    });
    const response = yield call(deleteSpecialTax, { id: payload });
    yield put({
        type: 'setMultiStore',
        payload: { pageLoading: false }
    });

    if (isSuccess(response)) {
        message.success(`【${currentRecord.name}】的专项扣款删除成功`);
        yield put({ type: 'search' });
    } else {
        message.error(response.msg);
    }

    if (callback) {
        callback(response);
    }
};

export default salarySpecialDeductModel;
